Skip to main content

mv command

mv - move (rename) files and directories

Changes the shell working directory.

Usage: mv [OPTION]... [-T] SOURCE DEST

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

  • OPTION: Flags which enhances the mv abilities.
  • SOURCE: The file or directory you want to copy.
  • DEST: The location where you want to copy.

Examples

  • Renaming a file

    $ mv oldfile.txt newfile.txt
    • Renames oldfile.txt to newfile.txt in the same directory.
    • If newfile.txt already exists, it will be overwritten without warning.

    Avoid Overwriting:

    If a file with the same name exists at the destination, mv will overwrite it without asking. To avoid this, use the -i (interactive) option which will prompt to confirm before overwriting.

    $ mv -i oldfile.txt newfile.txt
  • Moving a file

    To move a file to a different directory, specify the source file and the destination directory.

    $ mv document.txt /home/user/docs/
    • Moves document.txt from the current directory to /home/user/docs/.
    • The file keeps its original name unless you specify a new one.

    To move file with renaming it

    $ mv document.txt /home/user/docs/newdoc.txt
    • Moves and renames document.txt to newdoc.txt in /home/user/docs/.
  • Moving multiple files

    You can move multiple files to a directory by listing them before the destination.

    $ mv file1.txt file2.txt /home/user/backup/
    • Moves both file1.txt and file2.txt to /home/user/backup/.
    • Similarly we can use any number of files.
  • Moving directories

    Unlike cp, mv doesn’t need a recursive option to move directories, it handles them by default.

    $ mv /home/user/projects /home/user/backup/
    • Moves the entire projects directory and its contents to /home/user/backup/.
  • Verbose output

    Want to see what’s happening while the mv operation is running? Use -v (verbose) to display each action.

    $ mv -v file1.txt folder1
    'file1.txt' -> 'folder1/file1.txt'
  • Avoiding overwrites with conditions

    -u (update): Only move if the source is newer than the destination or if the destination doesnot exist.

    $ mv -u draft.txt /home/user/final/

    -n (no-clobber): Never overwrite an existing file.

    $ mv -n important.doc /home/user/archive/
    • Skips the move if important.doc already exists in /home/user/archive/.
  • Forcing the move

    Use -f (force) to overwrite without prompting, even if the destination exists.

    $ mv -f oldfile.txt /home/user/docs/
    • Overwrites oldfile.txt in /home/user/docs/ silently.
$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
-n, --no-clobber do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update move only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-Z, --context set SELinux security context of destination
file to default type
--help display this help and exit
--version output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups